package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Description:
 * @Title: EmployeeController
 * @Package com.itheima.reggie.controller
 * @Author: 🐟
 * @Copyright 🐟
 * @CreateTime: 2022/9/11 8:25
 */
@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        if(emp!=null){
            String pwd = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
            if(pwd.equals(emp.getPassword())){
                if(emp.getStatus()==1){
                    request.getSession().setAttribute("employee",emp.getId());
                    emp.setPassword(null);
                    return R.success(emp);
                }else {
                    return R.error("该用户已经被禁用");
                }
            }else {
                return R.error("密码错误，请检查");
            }
        }else {
            return R.error("不存在该用户，请检查");
        }
    }
    @PostMapping("/logout")
    public R<String> loginout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出登录成功");
    }
    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.save(employee);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Employee> Page = new Page<>();
        Page.setPages(page);
        Page.setSize(pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Employee::getUsername,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(Page,lqw);
        return R.success(Page);
    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> selectbyid(@PathVariable String id){
        Employee emp = employeeService.getById(id);
        return R.success(emp);

    }
}
